package com.loan.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.PayLogEntity;
import com.loan.system.mapper.PayLogMapper;
import com.loan.system.service.PayLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author shiguang on 2022/9/7 15:52
 */
@RequiredArgsConstructor
@Service
public class PayLogServiceImpl implements PayLogService {

    private final PayLogMapper payLogMapper;

    @Override
    public void saveLog(ApplyEntity applyEntity, String type, String bankOrderNo) {
        PayLogEntity payLogEntity = new PayLogEntity();
        payLogEntity.setApplyId(applyEntity.getId());
        payLogEntity.setType(type);
        payLogEntity.setPayMoney(applyEntity.getRealApplyMoney() - applyEntity.getApplyServiceCharge() - applyEntity.getRiskMoney() - applyEntity.getManageMoney());
        payLogEntity.setPayTime(applyEntity.getApplyTime());
        payLogEntity.setStatus("2");
        payLogEntity.setBankOrderNo(bankOrderNo);
        payLogEntity.setCompanyId(applyEntity.getCompanyId());
        payLogMapper.insert(payLogEntity);
    }

    @Override
    public void saveLogFail(ApplyEntity applyEntity, String remark) {
        PayLogEntity payLogEntity = new PayLogEntity();
        payLogEntity.setApplyId(applyEntity.getId());
        payLogEntity.setType("1");
        payLogEntity.setPayMoney(applyEntity.getRealApplyMoney() - applyEntity.getApplyServiceCharge() - applyEntity.getRiskMoney() - applyEntity.getManageMoney());
        payLogEntity.setPayTime(new Date());
        payLogEntity.setStatus("1");
        payLogEntity.setBankOrderNo(null);
        payLogEntity.setRemark(remark);
        payLogEntity.setCompanyId(applyEntity.getCompanyId());
        payLogMapper.insert(payLogEntity);
    }

    @Override
    public void saveLogSuccess(ApplyEntity applyEntity, String bankOrderNo) {
        PayLogEntity payLogEntity = new PayLogEntity();
        payLogEntity.setApplyId(applyEntity.getId());
        payLogEntity.setType("1");
        payLogEntity.setPayMoney(applyEntity.getRealApplyMoney() - applyEntity.getApplyServiceCharge() - applyEntity.getRiskMoney() - applyEntity.getManageMoney());
        payLogEntity.setPayTime(new Date());
        payLogEntity.setStatus("2");
        payLogEntity.setBankOrderNo(bankOrderNo);
        payLogEntity.setCompanyId(applyEntity.getCompanyId());
        payLogMapper.insert(payLogEntity);
    }

    @Override
    public void saveLogWait(ApplyEntity applyEntity,String remark) {
        PayLogEntity payLogEntity = new PayLogEntity();
        payLogEntity.setApplyId(applyEntity.getId());
        payLogEntity.setType("1");
        payLogEntity.setPayMoney(applyEntity.getRealApplyMoney() - applyEntity.getApplyServiceCharge() - applyEntity.getRiskMoney() - applyEntity.getManageMoney());
        payLogEntity.setPayTime(new Date());
        payLogEntity.setStatus("0");
        payLogEntity.setBankOrderNo(null);
        payLogEntity.setRemark(remark);
        payLogEntity.setCompanyId(applyEntity.getCompanyId());
        payLogMapper.insert(payLogEntity);
    }

    /**
     * 根据订单id获取最后一次打款记录
     * @param id
     * @return
     */
    @Override
    public PayLogEntity getByApplyId(Long id) {
        return payLogMapper.selectOne(new QueryWrapper<PayLogEntity>().eq("apply_id",id).eq("status","2").orderByDesc("create_time").last("limit 1"));
    }

    @Override
    public PayLogEntity getFailByApplyId(Long id) {
        return payLogMapper.selectOne(new QueryWrapper<PayLogEntity>()
                .eq("apply_id",id)
                .eq("status","1")
                .orderByDesc("create_time")
                .last("limit 1"));
    }
}
